package com.example.qinghe.frontController.communication;

import com.alibaba.fastjson.JSON;
import com.example.qinghe.entity.Common;
import com.example.qinghe.entity.communication.Staff;
import com.example.qinghe.service.communication.MessageInfoService;
import com.example.qinghe.service.communication.UserService;
import com.example.qinghe.util.MdsUtil;
import com.example.qinghe.util.Result;
import com.example.qinghe.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AdminController
 * 
 * @author ruoyi
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/front/message")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger

@Api(tags = "移动通讯录->聊天功能", description = "FrontMessageInfoController")
public class FrontMessageInfoController
{
    @Autowired
    private MessageInfoService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;



    @ApiOperation("聊天记录列表,显示最近的聊天记录，类似于QQ的消息列表")
    @GetMapping("/latestInfo")
    public Result latestInfo(
                             @ApiParam(name = "word",value = "关键词") @RequestParam(required = false) String word,
                         @ApiParam(name = "start",value = "从第几条数据开始查找") @RequestParam(required = false) int start,
                         @ApiParam(name = "limit",value = "几条数据") @RequestParam(required = false) int  limit, HttpServletRequest request
    )
    {
        Staff staff = userService.getStaff(request);
        if(staff==null)
            return Result.error("请先登录！");
        Long id=staff.getId();
        //Long id=1L;
        if(id<=0&&start<=0&&limit<=0)
            return Result.error("请检查传入的数据是否符合要求");
        Common common=new Common();
        common.put("ids",","+id+",");
        common.put("id",id);
        common.put("start",start);
        common.put("limit",limit);
        common.put("word",word);
        List<Common> commons = service.latestInfo(common);
        int unread = MdsUtil.infocount(commons, id);
        Map map = new HashMap<>();
        map.put("unread",unread);
        map.put("list",commons);
        System.out.println(JSON.toJSONString(map));
        return Result.success(map);
    }


    @ApiOperation("1对1聊天记录,显示两人之间的聊天记录")
    @GetMapping("/myinfo")
    public Result myInfo(@ApiParam(name = "toid",value = "和我聊天的人的id") @RequestParam(required = false) Long toid,
                         @ApiParam(name = "start",value = "从第几条数据开始查找") @RequestParam(required = false) int start,
                         @ApiParam(name = "limit",value = "几条数据") @RequestParam(required = false) int  limit, HttpServletRequest request
                         )
    {
        Staff staff = userService.getStaff(request);
        if(staff==null)
            return Result.error("请先登录！");
        Long id=staff.getId();
        //Long id=1L;

       if(toid<=0&&start<0&&limit<0)
           return Result.error("请检查传入的数据是否符合要求");
        Common common=new Common();
        common.put("fromid",id);
        common.put("toid",toid);
        common.put("start",start);
        common.put("limit",limit);
        List<Common> commons = service.myInfo(common);
        System.out.println(JSON.toJSONString(commons));
        if(commons.size()>0)
        {
            service.toread(commons.get(0),id);
            Collections.reverse(commons);
        }
        return Result.success(commons);
    }
    @ApiOperation("群聊记录，显示聊天群的消息")
    @GetMapping("/groupinfo")
    public Result groupinfo( @ApiParam(name = "toid",value = "聊天群id") @RequestParam(required = false) Long toid,
                         @ApiParam(name = "start",value = "从第几条数据开始查找") @RequestParam(required = false) int start,
                         @ApiParam(name = "limit",value = "几条数据") @RequestParam(required = false) int  limit, HttpServletRequest request
    )
    {
        Staff staff = userService.getStaff(request);
        if(staff==null)
            return Result.error("请先登录！");
        Long id=staff.getId();
        //Long id=1L;

        if(toid<=0&&start<0&&limit<=0)
            return Result.error("请检查传入的数据是否符合要求");
        Common common=new Common();
        common.put("toid",toid);
        common.put("start",start);
        common.put("limit",limit);
        List<Common> groupinfo = service.groupinfo(common);
        if(groupinfo.size()>0)
        {
            service.toread(groupinfo.get(0), id);
            Collections.reverse(groupinfo);
        }
        return Result.success(groupinfo);
    }
    @ApiOperation("删除聊天记录")
    @GetMapping("/del")
    public Result groupinfo(@ApiParam(name = "ids",value = "聊天记录的id") @RequestParam(required = true) Long ids, HttpServletRequest request)
    {
        Staff staff = userService.getStaff(request);
        if(staff==null)
            return Result.error("请先登录！");
        Long id=staff.getId();
        //Long id=1L;

        Common common = new Common();
        common.put("id",id);
        common.put("ids",ids);
        common.put("likeid",","+id+",");
        if(service.checkdel(common)<=0)
            return Result.error("这不是您发出的信息，无法删除");
        int del = service.del(ids);
        if(del!=-1)
        return Result.success("删除成功");
        return Result.error("删除失败");
    }
    @ApiOperation("多选删除聊天记录,聊天记录ids的数据格式是1,2,3")
    @GetMapping("/dels")
    public Result groupinfo(@ApiParam(name = "ids",value = "聊天记录的id") @RequestParam(required = true) String ids,HttpServletRequest request)
    {
        Staff staff = userService.getStaff(request);
        if(staff==null)
            return Result.error("请先登录！");
        Long id=staff.getId();
        //Long id=1L;

        String s = StrUtil.ListtoString(ids);
        Common common = new Common();
        common.put("id",id);
        common.put("ids",s);
        common.put("likeid",","+id+",");
        if(service.checkdels(common)>0)
            return Result.error("有信息不是您发出的或您的权限不足，无法删除");
        int del = service.dels(s);
        if(del!=-1)
            return Result.success("删除成功");
        return Result.error("删除失败");
    }




}
